package com.autonavi.minimap;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URLDecoder;
import java.util.Map;

import me.gfuil.bmap.uri.sdk.BmapHelper;
import me.gfuil.bmap.uri.sdk.Poi;
import me.gfuil.bmap.uri.sdk.StringUtils;

public class SplashActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = findViewById(R.id.text);

        if (null != getIntent().getDataString()) {
            text.setText(getIntent().getDataString());

            navi();
        }

        text.setOnClickListener(v -> copy());
        findViewById(R.id.btn_feedback).setOnClickListener(v -> feedback());
        findViewById(R.id.btn_navi).setOnClickListener(v -> navi());

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (null != intent) {
            TextView text = findViewById(R.id.text);
            text.setText(intent.getDataString());
            setIntent(intent);

            navi();
        }
    }

    public void navi() {
        try {
            String dataUri = URLDecoder.decode(getIntent().getDataString());
            if (StringUtils.isEmpty(dataUri)) {
                Toast.makeText(this, "未找到位置信息", Toast.LENGTH_SHORT).show();
                return;
            }

            Log.i("bmap_", "navi: " + dataUri);

            Map<String, String> map = StringUtils.getUrlRequest(dataUri);

            if (null == map || map.isEmpty()) {
                Toast.makeText(this, "未找到位置信息", Toast.LENGTH_SHORT).show();
                return;
            }

            if (dataUri.startsWith("wechatnav://")) {
                String[] fromCoord = StringUtils.convertStrToArray(map.get("fromcoord"), "[,]");
                String[] toCoord = StringUtils.convertStrToArray(map.get("tocoord"), "[,]");

                Poi start = null, end = null;
                if (null != fromCoord && fromCoord.length >= 2) {
                    start = new Poi();
                    start.setLatitude(StringUtils.getStringNumberDouble(fromCoord[0]));
                    start.setLongitude(StringUtils.getStringNumberDouble(fromCoord[1]));
                    if (null != map.get("from")) {
                        start.setName(map.get("from"));
                    } else {
                        start.setName("出发点");
                    }

                }
                if (null != toCoord) {
                    end = new Poi();
                    end.setLatitude(StringUtils.getStringNumberDouble(toCoord[0]));
                    end.setLongitude(StringUtils.getStringNumberDouble(toCoord[1]));
                    if (null != map.get("to")) {
                        end.setName(map.get("to"));
                    } else {
                        end.setName("目的点");
                    }
                }
                BmapHelper.route(this, BmapHelper.TYPE_MAP_DEFAULT, BmapHelper.TYPE_ROUTE_DEFAULT, start, end);
            } else {
                if (null != map.get("dlat") && null != map.get("dlon")) {
                    Poi start = null, end = null;
                    if (null != map.get("slat") && null != map.get("slon")) {
                        start = new Poi();
                        start.setLatitude(StringUtils.getStringNumberDouble(map.get("slat")));
                        start.setLongitude(StringUtils.getStringNumberDouble(map.get("slon")));
                        if (null != map.get("sname")) {
                            start.setName(map.get("sname"));
                        } else {
                            start.setName("目的点");
                        }

                    }
                    end = new Poi();
                    end.setLatitude(StringUtils.getStringNumberDouble(map.get("dlat")));
                    end.setLongitude(StringUtils.getStringNumberDouble(map.get("dlon")));
                    if (null != map.get("dname")) {
                        end.setName(map.get("dname"));
                    } else {
                        end.setName("目的点");
                    }

                    int typeRoute = BmapHelper.TYPE_ROUTE_DEFAULT;
                    if ("0".equals(map.get("t"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_DRIVE;
                    } else if ("1".equals(map.get("t"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_BUS;
                    } else if ("2".equals(map.get("t"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_WALK;
                    } else if ("3".equals(map.get("t"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_BIKE;
                    }
                    BmapHelper.route(this, BmapHelper.TYPE_MAP_DEFAULT, typeRoute, start, end);
                } else if (null != map.get("lat") && null != map.get("lon")) {
                    Poi poi = new Poi();
                    poi.setLongitude(StringUtils.getStringNumberDouble(map.get("lon")));
                    poi.setLatitude(StringUtils.getStringNumberDouble(map.get("lat")));
                    if (null != map.get("name")) {
                        poi.setName(map.get("name"));
                    }
                    BmapHelper.marker(this, poi);
                } else if (null != map.get("position") || null != map.get("center") || null != map.get("location")) {
                    String[] lnglat = StringUtils.convertStrToArray(map.get("position"), "[,]");
                    if (null == lnglat || lnglat.length < 2) {
                        lnglat = StringUtils.convertStrToArray(map.get("center"), "[,]");
                    }
                    if (null == lnglat || lnglat.length < 2) {
                        lnglat = StringUtils.convertStrToArray(map.get("location"), "[,]");
                    }
                    if (null != lnglat && lnglat.length >= 2) {
                        Poi poi = new Poi();
                        poi.setLongitude(StringUtils.getStringNumberDouble(lnglat[0]));
                        poi.setLatitude(StringUtils.getStringNumberDouble(lnglat[1]));
                        if (null != map.get("name")) {
                            poi.setName(map.get("name"));
                        }
                        BmapHelper.marker(this, poi);
                    }

                } else if (null != map.get("from") || null != map.get("to")) {
                    String[] from = StringUtils.convertStrToArray(map.get("from"), "[,]");
                    String[] to = StringUtils.convertStrToArray(map.get("to"), "[,]");

                    Poi start = null, end = null;
                    if (null != from && from.length >= 2) {
                        start = new Poi();
                        start.setLatitude(StringUtils.getStringNumberDouble(from[1]));
                        start.setLongitude(StringUtils.getStringNumberDouble(from[0]));

                        if (from.length == 3 && null != from[2]) {
                            start.setName(from[2]);
                        } else {
                            start.setName("出发点");
                        }

                    }
                    if (null != to) {
                        end = new Poi();
                        end.setLatitude(StringUtils.getStringNumberDouble(to[1]));
                        end.setLongitude(StringUtils.getStringNumberDouble(to[0]));

                        if (to.length == 3 && null != to[2]) {
                            end.setName(to[2]);
                        } else {
                            end.setName("目的点");
                        }
                    }
                    int typeRoute = BmapHelper.TYPE_ROUTE_DEFAULT;
                    if ("car".equals(map.get("mode"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_DRIVE;
                    } else if ("bus".equals(map.get("mode"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_BUS;
                    } else if ("walk".equals(map.get("mode"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_WALK;
                    } else if ("ride".equals(map.get("mode"))) {
                        typeRoute = BmapHelper.TYPE_ROUTE_BIKE;
                    }

                    BmapHelper.route(this, BmapHelper.TYPE_MAP_DEFAULT, typeRoute, start, end);
                } else if (null != map.get("poiid")) {
                    if (BmapHelper.getBmapVersionCode(this) < 159) {
                        Toast.makeText(this, "暂不支持", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    BmapHelper.openPoiID(this, BmapHelper.TYPE_MAP_AMAP, map.get("poiid"));
                } else {
                    Toast.makeText(this, "暂不支持的链接", Toast.LENGTH_SHORT).show();
                }
            }


            new Handler(Looper.getMainLooper()).postDelayed(this::finish, 1000);
        } catch (
                Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "错误：" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }

    public void copy() {
        if (!StringUtils.isEmpty(getIntent().getDataString())) {
            ClipboardManager myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            myClipboard.setPrimaryClip(ClipData.newPlainText("text", getIntent().getDataString()));
            Toast.makeText(SplashActivity.this, "链接已复制", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(SplashActivity.this, "无链接可复制", Toast.LENGTH_SHORT).show();
        }
    }

    public void feedback() {
        try {
            copy();
            Uri uri = Uri.parse(getString(R.string.link_feedback));
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "错误：" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}
